//
//  HAODE
//
//  Created by panquanReabam.com on 15/9/23.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//
#import "OrderDetailViewController.h"
#import "MJRefresh.h"
#import "MemberListViewController.h"
#import "ToResiduePayViewController.h"
#import "MembershipDetailsViewController.h"
#import "OrderDetailView.h"
#import "CommonDetailView.h"
#import "OrderDetailModel.h"
@interface OrderDetailViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UITextViewDelegate,UISearchBarDelegate,UIScrollViewDelegate>
@end
@implementation OrderDetailViewController
{
    UIButton* operationsBtn;
    CommonDetailView*CommonView;
    OrderDetailView*DetailView;
    UIScrollView*mainScroll;
    NSDictionary*dataDict;
    UIView*operationsView;
    NSArray*ColorArray;
    NSMutableArray* operationsArray;
}
@synthesize alt = alt;

-(void)BackAction:(UIButton *)sender{
   [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavi];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:self.orderId forKey:@"orderId"];
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"Business/Order/Detail" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            NSLog(@"-----------%@", severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                dataDict=severDataDict.dataDict;
                mainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,64, ScreenWidth, ScreenHeight)];
                [mainScroll setBackgroundColor: zuiqianhuiColor];
                mainScroll.delegate = self;
                mainScroll.userInteractionEnabled = YES;
                mainScroll.scrollEnabled = YES;
                mainScroll.showsVerticalScrollIndicator = FALSE;
                mainScroll.showsHorizontalScrollIndicator = FALSE;
                [self.view addSubview:mainScroll];
                CommonView = [[CommonDetailView alloc]init];
                OrderDetailModel*dataModel=[OrderDetailModel dataModelWithOrderDetailDict:severDataDict.dataDict];
                CommonView.model=dataModel;
                CommonView.frame=CGRectMake(0, 0, ScreenWidth, CommonView.heightView.bottom);
                [mainScroll addSubview:CommonView];
               
                DetailView = [[OrderDetailView alloc]initWithFrame:CGRectMake(0, CommonView.bottom, ScreenWidth,500)];
                OrderDetailModel*dataModel1=[OrderDetailModel dataModelWithOrderDetailDict:severDataDict.dataDict];
                DetailView.model=dataModel1;
                DetailView.frame=CGRectMake(0, CommonView.bottom, ScreenWidth, DetailView.heightView.bottom);
                [mainScroll addSubview:DetailView];
               [mainScroll setContentSize:CGSizeMake(ScreenWidth, DetailView.bottom)];
                }else{
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [Global showTextHudSub:requestResultStr view:self.view];
            }
        }else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:@"连接失败" view:self.view];
        }
    }];
}
-(void)edithuiyuan:(UIButton*)sender{
    MembershipDetailsViewController *detailViewController = [[MembershipDetailsViewController alloc] init];
    [self.navigationController pushViewController:detailViewController animated:YES];
}
-(void)operations:(UIButton*)sender{
    operationsArray=[[NSMutableArray alloc]init];
    if ([[dataDict objectForKey:@"operations"]count]>0) {
        operationsArray=[dataDict objectForKey:@"operations"];
        operationsView=[[UIView alloc]initWithFrame:CGRectMake(ScreenWidth-[[dataDict objectForKey:@"operations"]count]*168/2, 0, ScreenWidth, 110)];
        operationsView.backgroundColor=[UIColor clearColor];
        [mainScroll addSubview:operationsView];
        UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBoad)];
        singleFingerOne.numberOfTouchesRequired = 1; //手指数
        singleFingerOne.numberOfTapsRequired = 1; //tap次数
        singleFingerOne.delegate = self;
        [mainScroll addGestureRecognizer:singleFingerOne];

        ColorArray =[NSArray arrayWithObjects:[UIColor colorWithRed:90/255.0 green:130/255.0 blue:26/255.0 alpha:1.0],[UIColor colorWithRed:78/255.0 green:112/255.0 blue:22/255.0 alpha:1.0],nil];
        for (int i = 0; i<[[dataDict objectForKey:@"operations"]count]; i++) {
            UIButton *optName = [[UIButton alloc]initWithFrame:CGRectMake(168/2*i,0, 168/2, 110)];
            [optName setTitle:[operationsArray[i] objectForKey:@"optName"]  forState:UIControlStateNormal];
            optName.tag=i;
            [optName setTitleColor:[UIColor whiteColor] forState:0];
            optName.titleLabel.font=[UIFont systemFontOfSize:12];
            optName.backgroundColor=ColorArray[i];
            [optName addTarget:self action:@selector(btnTouchUpInsid:) forControlEvents:UIControlEventTouchUpInside];
            [operationsView addSubview:optName];
           }
    }

}
-(void)btnTouchUpInsid:(UIButton*)sender{
    [operationsView removeFromSuperview];
    if ([[operationsArray[sender.tag] objectForKey:@"optCode"] isEqualToString:@"gathering"]) {
        ToResiduePayViewController  *generateVC=[[ToResiduePayViewController alloc] init];
        generateVC.sat=@"fillPlaceOrder";
        generateVC.Dict=[operationsArray[sender.tag] objectForKey:@"params"];
//      generateVC.delegate=self;
        [self.navigationController pushViewController:generateVC animated:YES];
    }
    
    if ([[operationsArray[sender.tag] objectForKey:@"optCode"] isEqualToString:@"Delivery"]) {
//        DeliveryConfirmTheOrderViewController  *generateVC=[[DeliveryConfirmTheOrderViewController alloc] init];
////         generateVC.orderId=orderId[i
//        [self.navigationController pushViewController:generateVC animated:YES];
    }
    
}
-(void)closeKeyBoad
{
    [operationsView removeFromSuperview];
}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}
-(void)setNavi{
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBarHidden=NO;
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    self.view.backgroundColor=[UIColor whiteColor];
    self.title=@"订单详情";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor blackColor],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    

    operationsBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0,  60/2, 60/2)];
    [operationsBtn setTitle:@"..." forState:UIControlStateNormal];
    operationsBtn.titleLabel.font=[UIFont systemFontOfSize:28];
    [operationsBtn setTitleColor:shenhuiColor forState:UIControlStateNormal];
    [operationsBtn addTarget:self action:@selector(operations:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:operationsBtn];
    self.navigationItem.rightBarButtonItem = myBtnBtn;
    
    
}

@end
